package com.meituan.catering.management.product.biz.model.converter;

import com.meituan.catering.management.product.api.thrift.model.response.ProductDetailThriftResponse;
import com.meituan.catering.management.product.biz.model.ProductBO;
import java.util.stream.Collectors;

/**
 * 从其他数据模型向商品Http模型的转换器
 */
public abstract class ProductThriftVOConverter {

    /**
     * 根据BO转为 thriftVO
     * @return ProductDetailThriftResponse
     */
    public static ProductDetailThriftResponse fromBO(ProductBO productBO) {

        if (productBO == null) {
            return null;
        } else {
            ProductDetailThriftResponse response = new ProductDetailThriftResponse();
            response.setId(productBO.getId());
            response.setTenantId(productBO.getTenantId());
            response.setVersion(productBO.getVersion());
            if(productBO.getAuditing()!=null)
            {
                /*
                 *  时间转Long
                 */
                if (productBO.getAuditing().getCreatedAt()!=null)
                {
                    response.getAuditing().setCreatedAtTimestamp(productBO.getAuditing().getCreatedAt().getTime());
                }
                response.getAuditing().setCreatedBy(productBO.getAuditing().getCreatedBy());
                response.getAuditing().setLastModifiedBy(productBO.getAuditing().getLastModifiedBy());

                if(productBO.getAuditing().getLastModifiedAt()!=null)
                {
                    response.getAuditing().setLastModifiedAtTimestamp(productBO.getAuditing().getLastModifiedAt().getTime());
                }
            }

            response.setName(productBO.getName());

            if (productBO.getUnitPrice()!=null)
            {
                response.setUnitPrice(productBO.getUnitPrice().doubleValue());
            }
            response.setUnitOfMeasure(productBO.getUnitOfMeasure());
            if(productBO.getMinSalesQuantity()!=null)
            {
                response.setMinSalesQuantity(productBO.getMinSalesQuantity().doubleValue());
            }
            if(productBO.getIncreaseSalesQuantity()!=null)
            {
                response.setIncreaseSalesQuantity(productBO.getIncreaseSalesQuantity().doubleValue());
            }
            response.setDescription(productBO.getDescription());


            if (productBO.getEnabled() != null) {
                response.setEnabled(productBO.getEnabled() == 1);
            }
            response.setVersion(productBO.getVersion());

            response.getAccessoryGroups().addAll(productBO.getAccessoryGroups().stream().map(ProductThriftVOConverter::fromVOToAcc).collect(Collectors.toList()));
            response.getMethodGroups().addAll(productBO.getMethodGroups().stream().map(ProductThriftVOConverter::fromVOToMeth).collect(Collectors.toList()));
            /*
             * 转化 加料组 和方法组
             */
            return response;
        }
    }
        /**
         * 转化 加料
         * @param accessoryGroupBO BO模型
         * @return ProductDetailThriftResponse.AccessoryGroup
         */
        public static ProductDetailThriftResponse.AccessoryGroup fromVOToAcc(ProductBO.AccessoryGroup accessoryGroupBO){
            ProductDetailThriftResponse.AccessoryGroup accessoryGroupVO=new ProductDetailThriftResponse.AccessoryGroup();
            accessoryGroupVO.setName(accessoryGroupBO.getName());
            accessoryGroupVO.getOptions().addAll(accessoryGroupBO.getOptions().stream().map(ProductThriftVOConverter::fromVOToAccOption).collect(Collectors.toList()));
            return accessoryGroupVO;
        }
        public static ProductDetailThriftResponse.AccessoryGroup.Option fromVOToAccOption(ProductBO.AccessoryGroup.Option optionBO){
            ProductDetailThriftResponse.AccessoryGroup.Option optionVO=new ProductDetailThriftResponse.AccessoryGroup.Option();
            optionVO.setId(optionBO.getId());
            optionVO.setName(optionBO.getName());
            optionVO.setUnitPrice(optionBO.getUnitPrice().doubleValue());
            optionVO.setUnitOfMeasure(optionBO.getUnitOfMeasure());
            return optionVO;
        }


        /**
         * 转换做法组
         * @param methodGroupBO BO模型
         * @return ProductDetailThriftResponse.MethodGroup
         */

        public static ProductDetailThriftResponse.MethodGroup fromVOToMeth( ProductBO.MethodGroup methodGroupBO){
            ProductDetailThriftResponse.MethodGroup methodGroupVO=new ProductDetailThriftResponse.MethodGroup();

            methodGroupVO.setName(methodGroupBO.getName());
            methodGroupVO.getOptions().addAll(methodGroupBO.getOptions().stream().map(ProductThriftVOConverter::fromVOToMethOption).collect(Collectors.toList()));
            return methodGroupVO;
        }

        public static ProductDetailThriftResponse.MethodGroup.Option fromVOToMethOption(ProductBO.MethodGroup.Option optionBO){
            ProductDetailThriftResponse.MethodGroup.Option optionVO=new ProductDetailThriftResponse.MethodGroup.Option();
            optionVO.setId(optionBO.getId());
            optionVO.setName(optionBO.getName());
            return optionVO;
        }





    }
